﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Cashier.utils
{
    public class LogsUtil
    {
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="backStr"></param>
        public static void Record(Exception ex)
        {
            Thread thread = new Thread(SendMail);
            thread.Start(ex);
        }

        /// <summary>
        /// 发送异常邮件
        /// </summary>
        /// <param name="args"></param>
        private static void SendMail(Object args)
        {
            try
            {
                Exception ex = args as Exception;
                String stackTrace = ex.StackTrace;
                if (ToolsUtil.IsConnInternet())
                {
                    String localPath = System.Windows.Forms.Application.StartupPath;
                    System.Diagnostics.ProcessModule module = System.Diagnostics.Process.GetCurrentProcess().MainModule;
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("****************************异常文本****************************");
                    sb.AppendLine("【软件版本】：" + module.ModuleName + "-" + module.FileVersionInfo.FileVersion);
                    sb.AppendLine("【主机名称】：" + ToolsUtil.GetLocalIP());
                    sb.AppendLine("【出现时间】：" + DateTime.Now.ToString());
                    if (ex != null)
                    {
                        sb.AppendLine("【异常类型】：" + ex.GetType().Name);
                        sb.AppendLine("【异常信息】：" + ex.Message);
                        sb.AppendLine("【堆栈调用】：" + ex.StackTrace);
                    }
                    sb.AppendLine("***************************************************************");
                    MailUtil.Send("异常信息（" + module.FileVersionInfo.FileVersion + "）：" + ex.Message, sb.ToString());
                }
            }
            catch
            { }
        }
    }
}
